// pages/myCenter_note/myCenter_note.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailTitle: "",
    detailContent: ""
  },
  add_note: function () {
    wx.navigateTo({
      url: '/pages/myCenter_writeNote/myCenter_writeNote',
    })
  },
  //渲染笔记标题到note界面
  getTitle: function () {
    const token = wx.getStorageSync('token');
    wx.request({
      url: `${app.globalData.url}/note`,
      method: 'GET',
      dataType: 'json',
      header: {
        'content-type': 'text/json',
        'Authorization': token,
      },
      success: res => {
        // 获取数组传给arrayList，在wxml中循环渲染
        this.setData({
          arrayList: res.data,
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  //删除笔记
  delTitle: function (noteId) {
    const token = wx.getStorageSync('token');
    wx.request({
      url: `${app.globalData.url}/note/${noteId}`,
      method: 'DELETE',
      dataType: 'json',
      header: {
        'Authorization': token
      },
      success: res => {
        // console.log("delete note sucess")
        this.setData({
          arrayList: this.data.arrayList
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  //编辑笔记
  editNote: function (noteId) {
    const token = wx.getStorageSync('token');
    wx.request({
      url: `${app.globalData.url}/note/${noteId}`,
      method: 'GET',
      dataType: 'json',
      header: {
        'content-type': 'text/json',
        'Authorization': token
      },
      success: res => {
        // console.log(res)
        this.setData({
          detailTitle: res.data[0].noteTitle,
          detailId: res.data[0].noteId,
          detailContent: res.data[0].noteContent
        })
        console.log(res.data[0])
        wx.navigateTo({
          url: '/pages/myCenter_writeNote/myCenter_writeNote?noteTitle=' + this.data.detailTitle + '&noteId=' + this.data.detailId + '&noteContent=' + this.data.detailContent,
        })
      }
    })

  },
  //点击删除时，触发删除笔记事件
  slideButtonTap(e) {
    // console.log(e.detail.index);
    if (e.detail.index == 1) {
      this.data.arrayList.splice(e.currentTarget.dataset.index, 1)
      this.delTitle(e.currentTarget.id)
    } else if (e.detail.index == 0) {
      // console.log(e)
      // console.log("editing")
      this.editNote(e.currentTarget.id)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 没有登录时，提示用户不能登录
    if (app.globalData.isLogin == false) {
      wx.showModal({
        title: '未登录',
        content: '您还没有登录哦，快去登录吧！',
        confirmText: '去登录',
        cancelText: '暂不登录',
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/myCenter/myCenter'
            })
          }
        }
      })
    }
    this.getTitle();

    this.setData({
      slideButtons: [{
          text: '编辑',
          extClass: 'test',
          src: '/images/icon_star.svg', // icon的路径
        },
        {
          type: 'warn',
          text: '删除',
          extClass: 'test',
          src: '/images/icon_del.svg', // icon的路径
        }
      ],
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTitle()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})